
<?
/**
* Radio快速查询公共代码提取
* 2011-10-22 by ZhouHuan
*/

function get_date_range_map() {
    if(date("d") > 28) {
        $min_days = 28; // 按最小的28天计算天数，这样取上一月份才是准确的
    } else {
        $min_days = date("d"); 
    }
    $lastmonth = mktime(0, 0, 0, date("m")-1, $min_days, date("Y")); 
    return array(
        'today_start' => date("Y-m-d"),
        'today_end' => date("Y-m-d"),
        'yesterday_start' => date("Y-m-d", strtotime("-1 day")),
        'yesterday_end' => date("Y-m-d", strtotime("-1 day")),
        'prev_week_start' => date("Y-m-d", strtotime("-".(6+(date("w")==0 ? 7 : date("w")))." day")),
        'prev_week_end' => date("Y-m-d", strtotime("-".(date("w")==0 ? 7 : date("w"))." day")),
        'prev_month_start' => date("Y-m-01", $lastmonth),
        'prev_month_end' => date("Y-m-t", $lastmonth),
        'current_month_start' => date("Y-m-01"),
        'current_month_end' => date("Y-m-t"),
        'recent_7_days_start' => date("Y-m-d", strtotime("-7 day")),
        'recent_7_days_end' => date("Y-m-d")  
    );
}

?>

<?
/**************************  FUNCTION BEGIN ***********************/
function show_quick_searh_js() {
     // 好像全局变量无法引用，原因未知
     // $GLOBALS['date_range_map'];  
     // 改用function返回数组
     $date_range_map = get_date_range_map();
     foreach($date_range_map as $k=>$v) {
        echo "var $k = '$v'; \n";
     }
?>
	function quick_search(c) {
        if(c == "all") {       
            $('#begin_time').val('');
            $('#end_time').val('');    
        } else {
		    eval("$('#begin_time').val("+ c +"_start)");
		    eval("$('#end_time').val("+ c +"_end)");
        }
		if(typeof(quick_search_handler) == "function") {
			quick_search_handler(c);
		}
		search();
	}
<?
}	
/**************************  FUNCTION END ***********************/
?>



<?
/**************************  FUNCTION BEGIN ***********************/
function show_quick_searh_radio($names, $checkedIndex=0) {
	$map = array('今天'=>'today', '昨天'=>'yesterday', '最近7天'=>'recent_7_days',
     '上周'=>'prev_week','上月'=>'prev_month','本月'=>'current_month','全部'=>'all');
	$arr = split(",", $names);
	$i = 0;
	foreach($arr as $k) {
		if(array_key_exists($k, $map)) {
            $c = $map[$k];
			$checked = "";
			if($i++ == $checkedIndex) $checked = "checked";
			echo "<input type='radio' name='qs' value='$c' onclick='quick_search(this.value)' $checked>$k ";
		}
	}
}	
/**************************  FUNCTION END ***********************/
?>